<template>
  <div class="lease-order container">
    <navbar>
      历史订单
      <span slot="right" class="color-muted fz-12">共{{ total }}条</span>
    </navbar>
    <div class="mt-25">
      <Table :columns="columns"
             :data="list"
             :loading="loading"
             size="small"
             stripe
             width="100%"
             height="560"
      >
        <template slot="device"  slot-scope="{ row }">
          <div class="device-info">
            <c-image :url="row.logo" alt=""/>
            <div class="info">
              <Tooltip :content="row.equipmentName" theme="light">
                <div class="name line-clamp">{{ row.equipmentName }}</div>
              </Tooltip>
              <div class="labels">
                <span>原价{{row.cashMoney}}</span>
              </div>
            </div>
          </div>
        </template>
        <template slot="status" slot-scope="{ row }">
          <div class="status">
            <img :src="statusOptions[row.status].icon" alt="" class="status-icon">
            {{ statusOptions[row.status].label }}
          </div>
        </template>
        <template slot="money" slot-scope="{ row }">
          <span>¥{{ row.money }}</span>
        </template>
        <template slot="handler" slot-scope="{ row }">
          <div class="handler">
            <span class="p-5" v-if="row.status == 1"><i-button size="small" @click="changeStatus(row, 7)">取消</i-button></span>
            <span class="p-5"><i-button size="small" @click="handleDetail(row)">详情</i-button></span>
            <span class="p-5" v-if="row.status == 3"><i-button size="small" type="primary" @click="changeStatus(row, 4)">呼叫归还</i-button></span>
          </div>
        </template>
      </Table>
      <div class="lists-page tc mt-20" v-if="total">
        <Page :total="total"
              :current.sync="query.pageNum"
              :page-size="query.pageSize"
              @on-change="getList"/>
      </div>
    </div>
    <order-detail v-model="detail.show" :order="detail.data"/>
  </div>
</template>
<script>
import OrderDetail from "./components/orderDetail.vue";
import {leaseOrderList, leaseOrderChange} from "@/api/deviceLease";

export default {
  name: "order",
  components: {OrderDetail},
  data() {
    return {
      loading: false,
      columns: [
        {title: '设备信息', key: 'device', slot: 'device', width: 250, tooltip: true},
        {title: '状态', key: 'status', slot: 'status', align: 'center', minWidth: 100},
        {title: '租赁时长', key: 'hours', align: 'center', minWidth: 100},
        {title: '支付金额', key: 'money', slot: 'money', align: 'center', minWidth: 100},
        {title: '支付时间', key: 'payTime', align: 'center', minWidth: 140},
        {title: '结束时间', key: 'finishTime', align: 'center', minWidth: 140},
        {title: '操作', key: 'handler', slot: 'handler', align: 'center', minWidth: 150},
      ],
      statusOptions: {
        1: {label: '待支付', icon: require('@/assets/images/lease/status-1.png')},
        2: {label: '待配送', icon: require('@/assets/images/lease/status-2.png')},
        3: {label: '租赁中', icon: require('@/assets/images/lease/status-3.png')},
        4: {label: '待回收', icon: require('@/assets/images/lease/status-4.png')},
        5: {label: '已回收', icon: require('@/assets/images/lease/status-5.png')},
        6: {label: '已退款', icon: require('@/assets/images/lease/status-6.png')},
        7: {label: '已取消', icon: require('@/assets/images/lease/status-6.png')},
      },
      query: {
        pageNum: 1,
        pageSize: 10,
      },
      list: [],
      total: 10,
      detail: {
        show: false
      }
    }
  },
  computed: {},
  methods: {
    getList() {
      this.loading = true
      leaseOrderList(this.query).then(res => {
        this.list = (res.rows || []).map(item => {
          item.costMoney = JSON.parse(item.costMoney)
          item.hours = item.costMoney.hours+'小时'
          item.disPrice = item.costMoney.disPrice
          item.finishTime = item.finishTime || '-'
          return item;
        });
        this.total = res.total || 0;
      }).finally(() => {
        this.loading = false
      })
    },
    changeStatus(row, status) {
      const tips = {
        7: '确认取消租赁吗?',
        4: '确认呼叫归还吗?'
      }
      this.$cMessageBox.confirm(tips[status]).then(() => {
        leaseOrderChange({orderNumber: row.orderNumber, status: status}).then(res => {
          if (res.code == 200) {
            this.$Message.success('操作成功')
            this.getList()
          }
        })
      })
    },
    handleDetail(row) {
      this.detail = {
        show: true,
        data: row
      }
    }
  },
  created() {
    this.getList()
  },
  watch: {}
}
</script>

<style scoped lang="scss">
.lease-order {
  padding: 26px 20px;
  .device-info {
    @include flex_center;
    padding: 12px 0;
    .c-image {
      width: 56px!important;
      height: 56px!important;
      border-radius: 4px;
      border: 1px solid #F4F4F4;
    }
    .info {
      flex: 1;
      margin-left: 16px;
      .name {
        font-size: 13px;
      }
      .labels {
        display: flex;
        align-items: center;
        span {
          display: inline-block;
          height: 16px;
          font-size: 10px;
          line-height: 16px;
          padding: 0 4px;
          border: 1px solid $-color-orange;
          color: $-color-orange;
        }
      }
    }
  }
  .status {
    img {
      width: 16px;
      height: 16px;
      vertical-align: -3px;
    }
  }
  .handle span {
    display: inline-block;
  }
}
</style>
